<template>
  <el-dialog
    v-dialogDrag
    @close="handleClose"
    :title="'导出'"
    :lock-scroll="false"
    :visible="showDialog"
    :before-close="handleOpen"
    width="50%"
  >
    <el-form
      label-width="140px"
      :model="form"
      size="small"
      class="form"
      ref="formName"
    >
      <el-form-item
        label="模板："
        prop="templateId"
        :rules="[{ required: true, message: '请选择模板', trigger: 'change' }]"
      >
        <el-select
          class="w100"
          clearable
          v-model="form.templateId"
          collapse-tags
          filterable
          placeholder="请选择"
          size="mini"
          value-key="token"
        >
          <el-option
            v-for="item in templateListUniqBy"
            :key="item.token"
            :label="item.name + '-' + item.version"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="零件号：">
        <el-select
          class="w100"
          clearable
          v-model="form.partNumber"
          collapse-tags
          filterable
          placeholder="请选择"
          size="mini"
          value-key="token"
        >
          <el-option
            v-for="item in tableDataUniqBy('partNumber')"
            :key="item.partNumber"
            :label="item.partNumber"
            :value="item.partNumber"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="工序/工步号：">
        <el-select
          class="w100"
          clearable
          v-model="form.processNumber"
          collapse-tags
          filterable
          placeholder="请选择"
          size="mini"
          value-key="token"
        >
          <el-option
            v-for="item in tableDataUniqBy('processNumber')"
            :key="item.processNumber"
            :label="item.processNumber"
            :value="item.processNumber"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="工步号：">
        <el-select
          class="w100"
          clearable
          v-model="form.stepNumber"
          collapse-tags
          filterable
          placeholder="请选择"
          size="mini"
          value-key="token"
        >
          <el-option
            v-for="item in tableDataUniqBy('stepNumber')"
            :key="item.stepNumber"
            :label="item.stepNumber"
            :value="item.stepNumber"
          >
          </el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item label="工艺版次：">
        <el-select
          class="w100"
          clearable
          v-model="form.version"
          collapse-tags
          filterable
          placeholder="请选择"
          size="mini"
          value-key="token"
        >
          <el-option
            v-for="item in tableDataUniqBy('version')"
            :key="item.version"
            :label="item.version"
            :value="item.version"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态：">
        <el-select
          class="w100"
          clearable
          v-model="form.state"
          collapse-tags
          filterable
          placeholder="请选择"
          size="mini"
          value-key="token"
        >
          <el-option label="可用" value="true"> </el-option>
          <el-option label="禁用" value="false"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="日期：">
        <div class="flex-x">
          <el-date-picker
            class="w100"
            v-model="form.startDate"
            type="date"
            size="mini"
            placeholder="开始日期"
          ></el-date-picker>
          <i class="el-icon-right" style="color: rgba(0, 0, 0, 0.25)"></i>
          <el-date-picker
            class="w100"
            v-model="form.endDate"
            type="date"
            size="mini"
            placeholder="结束日期"
          ></el-date-picker>
        </div>
      </el-form-item>
      <el-form-item label="创建人：">
        <el-select
          class="w100"
          clearable
          v-model="form.author"
          collapse-tags
          filterable
          placeholder="请选择"
          size="mini"
        >
          <el-option
            v-for="item in userList"
            :key="item.username"
            :label="item.username"
            :value="item.username"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="btnLoading" size="mini" @click="handleClose"
        >取 消</el-button
      >
      <el-button
        :loading="btnLoading"
        size="mini"
        type="primary"
        @click="submitForm('formName')"
        >确 定</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import { exportTechnology } from "@/api/technology";
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: true
    },
    templateList: {
      type: Array,
      default: () => []
    },
    tableData: {
      type: Array,
      default: () => []
    },
    userList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      btnLoading: false,
      form: {
        version: "",
        author: "",
        partNumber: "",
        startDate: "",
        endDate: "",
        templateId: "",
        state: "",
        processNumber: "",
        stepNumber: ""
      }
    };
  },
  computed: {
    templateListUniqBy() {
      return _.uniqBy(this.templateList, "name");
    },
    tableDataUniqBy() {
      return function(key = "processNumber") {
        return _.uniqBy(this.tableData, key);
      };
    }
  },
  methods: {
    downloadFile(blob, fileName, fileUrl) {
      var link = document.createElement("a");
      var body = document.querySelector("body");
      link.href = window.URL.createObjectURL(blob);
      if (fileName) {
        let len = fileName.split(".").length;
        if (len == 1 && fileUrl) {
          let suffix = fileUrl.split(".").splice(-1, 1)[0];
          link.download = fileName + "." + suffix;
        } else {
          link.download = fileName;
        }
      }
      // fix Firefox
      link.style.display = "none";
      body.appendChild(link);
      link.click();
      body.removeChild(link);
      window.URL.revokeObjectURL(link.href);
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          exportTechnology(this.form).then(res => {
            var blob = new Blob([res.data], {
              type: "application/excel;chartset=UTF-8"
            });
            const fileName = `工艺配方文件-${this.$moment().format(
              "YYYY-MM-DD"
            )}.xlsx`;
            this.downloadFile(blob, fileName);
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    handleClose(isRefresh) {
      this.cancelEmpty();
      this.$emit("update:showDialog", false);
      this.$emit("closeDialog", isRefresh);
    },
    handleOpen() {
      this.cancelEmpty();
      this.handleClose();
    },
    cancelEmpty() {
      Object.assign(this.$data.form, this.$options.data().form);
    }
  }
};
</script>

<style></style>
